package cc.sunni.auto.controller;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

@RestController
public class TestController {
    @Value("${ttshitu.username}")
    private String username;
    @Value("${ttshitu.password}")
    private String password;

    @GetMapping("/test")
    public String test() {
        return username + " " + password;
    }

    public static void main(String[] args) throws IOException {
  /*      String url = "https://www.baacloud11.com/modules/_login.php";
        Map<String, Object> params = new HashMap<>();
        params.put("email", "31346337@qq.com");
        params.put("passwd", "jiangli3134");
        params.put("remember_me", "week");

        HttpResponse loginResponse = HttpRequest.post(url)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8")
                .form(params)
                .execute();

        // todo 判断:返回code:1 登录成功
        String body = loginResponse.body();
        List<HttpCookie> cookies = loginResponse.getCookies();
        StringJoiner sj = new StringJoiner("; ");
        for (HttpCookie cookie : cookies) {
            sj.add(cookie.toString());
        }
        // user_email=31346337%40qq.com; uid=1662800; user_pwd=7f2ae3b242bb4c4909ebb0c899b24bdd469664a40e8be; PHPSESSID=m0bn8u5e65dpcegs1m3tooucb0
        String cookie = sj.toString();*/

        Map<String, Object> params = new HashMap<>();
        String cookie = "user_email=31346337%40qq.com; uid=1662800; user_pwd=7f2ae3b242bb4c4909ebb0c899b24bdd469664a40e8be; PHPSESSID=lnamkvekojea4gq1ur29o0mp74";
        // 获取验证码
        HttpResponse captchaResponse = HttpRequest.get("https://www.baacloud11.com/other/captcha.php")
                .header("cookie", cookie)
                .execute();
        byte[] imgData = captchaResponse.bodyBytes();
        BufferedImage image = removeBackground(imgData);

        params.clear();
        params.put("username", "31346337");
        params.put("password", "jiangli3134");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "png", out);
        byte[] data = out.toByteArray();
        String imageBase64 = new String(Base64.encodeBase64(data));
        params.put("image", imageBase64);
        // 统一识别接口
        String url1 = "http://api.ttshitu.com/predict";
        String body1 = HttpRequest.post(url1)
                .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
                .body(JSONUtil.toJsonStr(params))
                .execute().body();
        JSON json = JSONUtil.parse(body1);
        Boolean success = json.getByPath("success", Boolean.class);
        String captcha = "";
        if (success) {
            captcha = json.getByPath("data.result", String.class);
        }

        System.out.println("captcha = " + captcha);
        params.clear();
        params.put("captcha", captcha);
        // 提交验证码 GET https://www.baacloud11.com/modules/_checkin.php?captcha=CDM3
        String body2 = HttpRequest.get("https://www.baacloud11.com/modules/_checkin.php?captcha=" + captcha)
                .header("cookie", cookie)
                .execute().body();

        System.out.println(body2);
    }

    public static BufferedImage removeBackground(byte[] imgData) throws IOException {
        int grayValue = 135;
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgData));
        int width = img.getWidth();
        int height = img.getHeight();

        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                int p = img.getRGB(x, y);
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                if (r * 0.299 + g * 0.578 + b * 0.114 >= grayValue) {
                    img.setRGB(x, y, Color.WHITE.getRGB());
                } else {
                    img.setRGB(x, y, Color.BLACK.getRGB());
                }
            }
        }
        return img;
    }
}
